/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.workbench.tests.adapters;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.reliablesource.model.core.IReliableElement;


/**
 *
 * MockObjPropertySourceAdapter.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class MockObjPropertySourceAdapter implements IPropertySource,
    IAdaptable {
    private static final String DEFAULT_VALUE = "<Value has not been set for this property.>";
    private IReliableElement adaptedElement;

    public MockObjPropertySourceAdapter() {
        //
    }

    public MockObjPropertySourceAdapter(IReliableElement _toAdapt) {
        this();
        adaptedElement = _toAdapt;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
     */
    public Object getEditableValue() {
        return this;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
     */
    public IPropertyDescriptor[] getPropertyDescriptors() {
        int _nProps_ = adaptedElement.getAvailableProperties().size();
        List<String> _propList_ = adaptedElement.getAvailableProperties();

        List<PropertyDescriptor> descriptorList = new ArrayList<PropertyDescriptor>();

        for (int i = 0; i < _nProps_; i++) {
            if (!(_propList_.get(i).equals(IReliableElement.PARENT))) {
                PropertyDescriptor propDesc = new PropertyDescriptor(_propList_.get(
                            i), _propList_.get(i));

                if (getPropertyValue(_propList_.get(i)).toString().length() < 1)
                    propDesc.setCategory("No Value ");
                else
                    descriptorList.add(propDesc);
            }
        }

        return descriptorList.toArray(new IPropertyDescriptor[descriptorList.size()]);
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
     */
    public Object getPropertyValue(final Object _id) {
        List<String> _propList_ = adaptedElement.getAvailableProperties();

        if (_propList_.contains(_id))
            if (adaptedElement.getProperty((String) _id).toString().length() > 0)
                return adaptedElement.getProperty((String) _id);

        return DEFAULT_VALUE;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
     */
    public boolean isPropertySet(Object id) {
        List<String> propList = adaptedElement.getAvailableProperties();

        if (propList.contains(id))
            return true;

        return false;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
     */
    public void resetPropertyValue(Object id) {
        //	
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
     */
    public void setPropertyValue(Object id, Object value) {
        //
    }

    /*
     *
     */
    public void setObjectToAdapt(IReliableElement _toAdapt) {
        Assert.isNotNull(_toAdapt);
        adaptedElement = _toAdapt;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
     */
    @SuppressWarnings("unchecked")
    public Object getAdapter(Class adapter) {
        return Platform.getAdapterManager().getAdapter(adaptedElement, adapter);
    }
}
